{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-11-03T05:51:32.460092300Z",
     "start_time": "2023-11-03T05:51:04.824408100Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import matplotlib.pyplot as plt\n",
    "from torchvision import datasets,transforms\n",
    "import torchvision"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([1, 28, 28, 60000])"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_path = '../data'\n",
    "trainset = datasets.MNIST(data_path,download=False,train=True,transform=transforms.ToTensor())\n",
    "imgs=torch.stack([img for img,_ in trainset],dim=3)\n",
    "imgs.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T05:55:28.548430500Z",
     "start_time": "2023-11-03T05:55:18.680560100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([47040000])\n"
     ]
    },
    {
     "data": {
      "text/plain": "(tensor(0.1307), tensor(0.3081))"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs_value = imgs.view(28*28*60000)\n",
    "print(imgs_value.shape)\n",
    "torch.mean(imgs_value,dim=0),torch.std(imgs_value,dim=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T05:59:51.750750100Z",
     "start_time": "2023-11-03T05:59:51.645756500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "data_transforms = {\n",
    "    'train':transforms.Compose([\n",
    "        transforms.ToTensor(),\n",
    "        transforms.Normalize((0.1307),(0.3081))\n",
    "    ]),\n",
    "    'test':transforms.Compose([\n",
    "        transforms.ToTensor(),\n",
    "        transforms.Normalize((0.1307),(0.3081))\n",
    "    ])\n",
    "}\n",
    "\n",
    "trainset = datasets.MNIST(data_path,download=False,transform=data_transforms['train'],train=True)\n",
    "testset = datasets.MNIST(data_path,download=False,transform=data_transforms['test'],train=False)\n",
    "\n",
    "train_loader = torch.utils.data.DataLoader(trainset,batch_size=4,shuffle=True,num_workers=2)\n",
    "test_loader = torch.utils.data.DataLoader(testset,batch_size=4,shuffle=True,num_workers=2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T10:50:57.268704Z",
     "start_time": "2023-11-03T10:50:57.157701200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "def imshow(inputs):\n",
    "    inputs = inputs/2+0.5\n",
    "    inputs = inputs.numpy().transpose(1,2,0)\n",
    "    plt.imshow(inputs)\n",
    "    plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T10:50:59.634309800Z",
     "start_time": "2023-11-03T10:50:59.616206200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).\n"
     ]
    },
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiYAAACxCAYAAADwMnaUAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8pXeV/AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAbI0lEQVR4nO3dfXSMd/r48Ws8ZCREujhmMg02TlN0U5aklDpES1psVbW7HrZo7dkiosIehOjKWhKrZ1N1lC5V7LE21nqo7eIktkQdVY9pPWzR05QoabalSephgny+f/SX+flMIslk7s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9L1gHtjQg1r5l//+peKjo5WVqtVde7cWa1atUrbTx2rVlxcrKZNm6bat2+vmjVrpjp27KhSUlK0N39qqNu7d2+l74Hjx49XStWsXjdv3lSJiYmqVatWKjg4WP3iF79QFy9e9MNX4z9V1TEvL+++v2v27t3rGsOIOlqUUsrT6RwAAABfMN0aEwAA0HDRmAAAANOgMQEAAKZBYwIAAEyDxgQAAJgGjQkAADANGhMAAGAaNCYAAMA0aEwAAIBp0JgAAADToDEBAACmQWMCAABM4/8AVneAxMqHYJAAAAAASUVORK5CYII="
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0)\n",
      "tensor(9)\n",
      "tensor(2)\n",
      "tensor(4)\n"
     ]
    }
   ],
   "source": [
    "imgs,labels = next(iter(train_loader))\n",
    "imshow(torchvision.utils.make_grid(imgs))\n",
    "for i in labels:\n",
    "    print(i)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T10:51:07.253768700Z",
     "start_time": "2023-11-03T10:51:02.856395600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 定义LeNet"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [],
   "source": [
    "import torch.nn.functional as F\n",
    "class LeNet(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.c1 = nn.Conv2d(1,6,5)\n",
    "        self.c3 = nn.Conv2d(6,16,5)\n",
    "        self.fc1 = nn.Linear(16*4*4,120)\n",
    "        self.fc2 = nn.Linear(120,84)\n",
    "        self.fc3 = nn.Linear(84,10)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(F.relu(self.c1(x)),2)\n",
    "        out = F.max_pool2d(F.relu(self.c3(out)),2)\n",
    "        out = out.view(-1,16*4*4)\n",
    "        out = F.relu(self.fc1(out))\n",
    "        out = F.relu(self.fc2(out))\n",
    "        out = F.relu(self.fc3(out))\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T11:14:18.761911900Z",
     "start_time": "2023-11-03T11:14:18.722914100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "def train_loop(model,loss_fn,optimizer,n_epochs,train_loader):\n",
    "    for epoch in  range(1,n_epochs+1):\n",
    "        loss_train = 0.0\n",
    "        for i,data in enumerate(train_loader,1):\n",
    "            imgs,labels = data\n",
    "            imgs,labels = imgs.cuda(),labels.cuda()\n",
    "            outputs = model(imgs)\n",
    "            loss = loss_fn(outputs,labels)\n",
    "            optimizer.zero_grad()\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "            loss_train+=loss.item()\n",
    "            if i%1000==0:\n",
    "                print('Epoch:{}, i:{}, 训练损失:{}'.format(epoch,i,loss_train/1000))\n",
    "                loss_train=0.0\n",
    "    print('训练结束')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T11:14:19.536831900Z",
     "start_time": "2023-11-03T11:14:19.504833600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch:1, i:1000, 训练损失:1.4260351032987237\n",
      "Epoch:1, i:2000, 训练损失:0.5338062929070438\n",
      "Epoch:1, i:3000, 训练损失:0.42550841081963153\n",
      "Epoch:1, i:4000, 训练损失:0.3937189310464019\n",
      "Epoch:1, i:5000, 训练损失:0.3415354207285272\n",
      "Epoch:1, i:6000, 训练损失:0.3684577816632591\n",
      "Epoch:1, i:7000, 训练损失:0.3514150243316144\n",
      "Epoch:1, i:8000, 训练损失:0.3577753896106042\n",
      "Epoch:1, i:9000, 训练损失:0.31510000343201316\n",
      "Epoch:1, i:10000, 训练损失:0.32361367395508933\n",
      "Epoch:1, i:11000, 训练损失:0.332689940274533\n",
      "Epoch:1, i:12000, 训练损失:0.30735289830324974\n",
      "Epoch:1, i:13000, 训练损失:0.3070854859165338\n",
      "Epoch:1, i:14000, 训练损失:0.3055936270640377\n",
      "Epoch:1, i:15000, 训练损失:0.3013439661194752\n",
      "Epoch:2, i:1000, 训练损失:0.28467371464960844\n",
      "Epoch:2, i:2000, 训练损失:0.30087377090100914\n",
      "Epoch:2, i:3000, 训练损失:0.28193104957161225\n",
      "Epoch:2, i:4000, 训练损失:0.2690092951168729\n",
      "Epoch:2, i:5000, 训练损失:0.2945109611428306\n",
      "Epoch:2, i:6000, 训练损失:0.3016261230244118\n",
      "Epoch:2, i:7000, 训练损失:0.30613282010643345\n",
      "Epoch:2, i:8000, 训练损失:0.2687983941032901\n",
      "Epoch:2, i:9000, 训练损失:0.2919063344940465\n",
      "Epoch:2, i:10000, 训练损失:0.2793371982990084\n",
      "Epoch:2, i:11000, 训练损失:0.3080094040843264\n",
      "Epoch:2, i:12000, 训练损失:0.2951224223293136\n",
      "Epoch:2, i:13000, 训练损失:0.2724606551372449\n",
      "Epoch:2, i:14000, 训练损失:0.2633859668110802\n",
      "Epoch:2, i:15000, 训练损失:0.2904165343479399\n",
      "训练结束\n"
     ]
    }
   ],
   "source": [
    "import torch.optim as optim\n",
    "lenet = LeNet().cuda()\n",
    "optimizer = optim.SGD(lenet.parameters(),lr=0.001,momentum=0.9)\n",
    "loss_fn = nn.CrossEntropyLoss()\n",
    "train_loop(model=lenet,optimizer=optimizer,loss_fn=loss_fn,train_loader=train_loader,n_epochs=2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T11:16:21.633233500Z",
     "start_time": "2023-11-03T11:14:20.404029300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [
    "def test_loop(model,test_loader):\n",
    "    correct =0\n",
    "    total = 0\n",
    "    with torch.no_grad():\n",
    "        for data in test_loader:\n",
    "            imgs,labels = data\n",
    "            imgs,labels = imgs.cuda(),labels.cuda()\n",
    "            outputs = model(imgs)\n",
    "            _,preds = torch.max(outputs,dim=1)\n",
    "            correct+=int((labels==preds).sum())\n",
    "            total+= labels.shape[0]\n",
    "    print('模型精度:{:.3f}%'.format(correct/total*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T11:24:40.402161900Z",
     "start_time": "2023-11-03T11:24:40.374160700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型精度:88.840%\n"
     ]
    }
   ],
   "source": [
    "test_loop(lenet.eval(),test_loader=test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T11:25:28.246177200Z",
     "start_time": "2023-11-03T11:25:16.078487700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型精度:88.872%\n"
     ]
    }
   ],
   "source": [
    "test_loop(lenet.eval(),test_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T11:26:32.746163Z",
     "start_time": "2023-11-03T11:25:46.858157400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "真实值-预测值\n",
      "0 --- 0\n",
      "9 --- 9\n",
      "2 --- 2\n",
      "4 --- 4\n"
     ]
    },
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiYAAACxCAYAAADwMnaUAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9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nTRs2aNct1DDXUlZSUqBMnTqgTJ04oEVEZGRnqxIkTrk+L1KRekyZNUhEREWrPnj3q+PHj6sknn2xwHxeuqo63b99Ww4YNUxERESo3N1f7XeN0Ol1jGFFHUzYmSin19ttvqw4dOqigoCDVo0cP10dfUZGIVPpau3at65iysjI1f/58ZbfbldVqVf369VMnT570X9L1gHtjQg1r5l//+peKjo5WVqtVde7cWa1atUrbTx2rVlxcrKZNm6bat2+vmjVrpjp27KhSUlK0N39qqNu7d2+l74Hjx49XStWsXjdv3lSJiYmqVatWKjg4WP3iF79QFy9e9MNX4z9V1TEvL+++v2v27t3rGsOIOlqUUsrT6RwAAABfMN0aEwAA0HDRmAAAANOgMQEAAKZBYwIAAEyDxgQAAJgGjQkAADANGhMAAGAaNCYAAMA0aEwAAIBp0JgAAADToDEBAACmQWMCAABM4/8AVneAxMqHYJAAAAAASUVORK5CYII="
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "outputs = lenet(imgs.cuda())\n",
    "_,preds = torch.max(outputs,dim=1)\n",
    "print('真实值-预测值')\n",
    "for i,j in zip(labels,preds):\n",
    "    print(i.item(),'---',j.item())\n",
    "imshow(torchvision.utils.make_grid(imgs))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T11:38:15.441779200Z",
     "start_time": "2023-11-03T11:38:15.240772100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
